<script type="text/javascript" src="../scripts/date.js"></script>
<script type="text/javascript">
	function guiform() 
	{  
		 document.formgui.submit(); 
	}
	function jsmaphongban(){   
		MaPB = $("#MaPB").val();
		$.post( 
				 "../adm/nhanvien/getmanguoidung.php",
				 { MaPB:  MaPB },
				 function(data) {
					$('#showmanguoidung').html(data);
					
				 }
		  
		);
	}
	function ChuyenMaKhachHang(MaKH){
		document.getElementById('MaKH').value = MaKH;
	}
	function GetThongTinKhachHang(MaKH){
		var xmlHttp;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            var thongTin = xmlHttp.responseText;
	            var mangThongTin = thongTin.split("|");
	            document.getElementById('NgaySinh').value = mangThongTin[0];
	            document.getElementById('DiaChi').value = mangThongTin[1];
	            document.getElementById('SoCMND').value = mangThongTin[2];
	            document.getElementById('NgayCap').value = mangThongTin[3];
	            document.getElementById('NoiCap').value = mangThongTin[4];
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/taisanthechap/getthongtinkhachhang.php?makh='+MaKH, true);
	    xmlHttp.send(null);
	}
	function getKyHan(MaKyHan){
	 	var xmlHttp;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            document.getElementById('KyHan').value = xmlHttp.responseText;
	            if(xmlHttp.responseText=="0") {
	            	 document.getElementById("MaHTLanhLai").disabled="disabled";
	            	 var currentDate = new Date();
	 	    	  	 var day = currentDate.getDate();
	 	    	  	 var month = currentDate.getMonth() + 1;
	 	    	  	 var year = currentDate.getFullYear();
	            	 document.getElementById("NgayLanhLai").value=year + "-" + month + "-" + day;
	            }
	            else {
	            	document.getElementById("MaHTLanhLai").removeAttribute("disabled");
	            }
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/sotietkiem/getkyhan.php?makyhan='+MaKyHan, true);
	    xmlHttp.send(null);
	}

	function ganNgayHieuLuc(MaKyHan){
		var xmlHttp;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            var KyHan = xmlHttp.responseText;
	    		var currentDate = new Date();
	    	  	var day = currentDate.getDate();
	    	  	var month = currentDate.getMonth() + 1;
	    	  	var year = currentDate.getFullYear();
	    		document.getElementById('NgayMoSo').value = year + "-" + month + "-" + day;
	    		var ngayToiHan = Date.today().addMonths(Number(KyHan)); 
	    		document.getElementById('NgayDenHan').value = ngayToiHan.getFullYear()+"-"+(ngayToiHan.getMonth()+1)+"-"+ngayToiHan.getDate();
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/sotietkiem/getkyhan.php?makyhan='+MaKyHan, true);
	    xmlHttp.send(null);
	}
	function ajaxFunction(){
	    var xmlHttp;
	    var MaKyHan = document.getElementById('MaKyHan').value;
	    var HinhThucThangNam = document.getElementById('HinhThucThangNam').value;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            document.getElementById('LaiSuat').value = xmlHttp.responseText;
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/sotietkiem/getlaisuat.php?makyhan='+MaKyHan+'&hinhthuc='+HinhThucThangNam, true);
	    xmlHttp.send(null);
	}
	function checkHTLanhLai() {
		 var xmlHttp;
	    var MaKyHan = document.getElementById('MaKyHan').value;
	    var MaHTLanhLai = document.getElementById('MaHTLanhLai').value;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            var ketQuaCheck = xmlHttp.responseText;
	     			if(ketQuaCheck=="false")
	     			{
		            	alert('Hình thức lãnh lãi không được lớn hơn kỳ hạn');
		            	document.getElementById('MaHTLanhLai').value = 0;
		            	document.getElementById('NgayLanhLai').value = "";
	     			}
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/sotietkiem/checkhinhthuclanhlai.php?makyhan='+MaKyHan+'&htlanhlai='+MaHTLanhLai, true);
	    xmlHttp.send(null);
	}
	function ganNgayLanhLai(){
		 var MaHTLanhLai = document.getElementById('MaHTLanhLai').value;
		var xmlHttp;
	    if(window.XMLHttpRequest){
	        xmlHttp = new XMLHttpRequest;
	    }
	    else if(window.ActiveXObject){
	        xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
	    }
	    
	    xmlHttp.onreadystatechange = function(){
	        if(xmlHttp.readyState == 4){
	            var HTLanhLai = xmlHttp.responseText;
	    		var ngayToiHan = Date.today().addMonths(Number(HTLanhLai)); 
	    		document.getElementById('NgayLanhLai').value = ngayToiHan.getFullYear()+"-"+(ngayToiHan.getMonth()+1)+"-"+ngayToiHan.getDate();
	        }
	    }
	    xmlHttp.open('GET', '../adm/nhanvien/sotietkiem/gethtlanhlai.php?mahtlanhlai='+MaHTLanhLai, true);
	    xmlHttp.send(null);
	}
</script>

<h2><strong>Mở sổ tiết kiệm</strong></h2>
<br/>
<form enctype="multipart/form-data" name="formgui"  method="post" action="index.php?do=mosotietkiem_xuly" onsubmit="return checkPostArtForm(this);">
<table style="width:950px; margin-left: 10px; table-layout: fixed;">
		<tr>
			<td style="width:20px;padding-top:10px;">
	        	<label for="masotietkiem">Số sổ:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <input readonly="readonly" tabindex="1" type="text" name="MaSoTietKiem" class="txt" size="54" value="<?php include 'getmasotietkiem.php';?>"/>   
	        </td>
	        <td style="width:20px;padding-top:10px;">

	        </td>
	        <td style="width:60px;padding-top:10px;">

	        </td>
		</tr>
		<tr>
			<td style="padding-top:10px;">
	        	<label for="tenkhachhang">Khách hàng:</label>&nbsp<span class="requirefield">*</span>         	
	        </td>	       
			<td style="padding-top:10px;">
				<select name="TenKhachHang" onchange="ChuyenMaKhachHang(this.value);GetThongTinKhachHang(this.value);">	
            		<option value="0">-- Chọn khách hàng --</option>	                      	
					<?php
						$khachhang = new khachhangbs();
						$danhsachkhachhang = $khachhang->LayDanhSach();
						for($j = 0; $j < ($danhsachkhachhang != false ? count($danhsachkhachhang) : 0); $j++)
						{	
							$HoTen = $danhsachkhachhang[$j]['Ho'] . " "	. $danhsachkhachhang[$j]['Ten'];	
							echo "<option value='{$danhsachkhachhang[$j]["MaKH"]}'>{$HoTen}</option>";
						}
					?>
				</select>
				&nbsp <input onclick="location.href = 'index.php?do=khachhang';" class="buton" type="button" name="button" id="btndskhachhang" style="margin-left: 5px; cursor: pointer; background: url('images/list_add.png') no-repeat top left;" />
			</td>
		</tr>
		<tr>
			<td style="padding-top:10px;">
	        	<label for="makhachhang">Tài khoản:</label>      	
	        </td>
			 <td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="MaKH" name="MaKH" class="txt" size="12" value="0" /> 
	        </td>
	        <td style="padding-top:10px;">
	        	<label for="ngaysinh">Ngày sinh:</label>            	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="NgaySinh" name="NgaySinh" class="txt" size="12" value="<?php echo $dskhachhang[0]["NgaySinh"];?>" /> 
	        </td>
		</tr>
		<tr>
			<td style="padding-top:10px;">
	        	<label for="diachi">Địa chỉ:</label>         	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="DiaChi" name="DiaChi" class="txt" size="12" value="<?php echo $dskhachhang[0]["DiaChi"];?>" /> 
	        </td>
			<td style="padding-top:10px;">
	        	<label for="socmnd">Số CMND:</label>        	
	        </td>
			 <td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="SoCMND" name="SoCMND" class="txt" size="12" value="<?php echo $dskhachhang[0]["SoCMND"];?>" /> 
	        </td>
		</tr>
		<tr>
			<td style="padding-top:10px;">
	        	<label for="ngaycap">Ngày cấp:</label>          	
	        </td>	       
			<td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="NgayCap" name="NgayCap" class="txt" size="12" value="<?php echo $dskhachhang[0]["NgayCap"];?>" /> 
	        </td>
			<td style="padding-top:10px;">
	        	<label for="noicap">Nơi cấp:</label>        	
	        </td>
			 <td style="padding-top:10px;">
	        	<input readonly="readonly" tabindex="1" type="text" id="NoiCap" name="NoiCap" class="txt" size="12" value="<?php echo $dskhachhang[0]["NoiCap"];?>" /> 
	        </td>
		</tr>
		<tr>
			<td align="center" colspan="0">
			<hr>
			</td>
		</tr>				
		<tr>
			<td style="padding-top:10px;">
	        	<label for="loaitietkiem">Loại kỳ hạn:</label>&nbsp<span class="requirefield">*</span> 
	        </td>
			<td style="padding-top:10px;">
				<select name="MaKyHan" onchange="ajaxFunction();getKyHan(this.value);ganNgayHieuLuc(this.value);checkHTLanhLai(this.value);" id ="MaKyHan">	
            		<option value="0">-- Chọn kỳ hạn --</option>	                      	
					<?php
						$kyhan = new loaikyhanbs();
						$danhsachkyhan = $kyhan->LayDanhSach();
						for($j = 1; $j < ($danhsachkyhan != false ? count($danhsachkyhan) : 1); $j++)
						{		
							echo "<option value='{$danhsachkyhan[$j]["MaLoaiKyHan"]}'>{$danhsachkyhan[$j]["TenLoaiKyHan"]}</option>";
						}
					?>
				</select>			
			</td>
			<td style="padding-top:10px;">
				<label for="laisuat">Lãi suất:</label>
	        </td>
			<td style="padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="LaiSuat" name="LaiSuat" class="input" size="12" value="0" />%	       
		        &nbsp
		        <select id ="HinhThucThangNam" name="HinhThucThangNam" onchange="ajaxFunction();">	                      	
						<option value="Thang">Tháng</option>
						<option value="Nam">Năm</option>
				</select>
			 </td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="kyhan">Kỳ hạn:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <input readonly="readonly" tabindex="1" type="text" id="KyHan" name="KyHan" class="txt" size="12" value="0" />
	        </td>
		</tr>
		<tr>		
	         <td style="width:20px;padding-top:10px;">
				<label for="mahtlanhlai">Hình thức lãnh lãi: <?php echo $danhsachkyhan[$j]["MaHTLanhLai"];?></label>&nbsp<span class="requirefield">*</span> 
	        </td>
	        <td style="width:60px;padding-top:10px;">
				<select name="MaHTLanhLai" id="MaHTLanhLai" onchange="checkHTLanhLai(this.value);ganNgayLanhLai(this.value);">	
            		<option value="0">-- Chọn hình thức lãnh lãi --</option>	                      	
					<?php
						$hinhthuclanhlai = new hinhthuclanhlaibs();
						$danhsachhinhthuclanhlai = $hinhthuclanhlai->LayDanhSach();
						for($j = 0; $j < ($danhsachhinhthuclanhlai != false ? count($danhsachhinhthuclanhlai) : 0); $j++)
						{
							if($danhsachhinhthuclanhlai[$j]["MaHTLanhLai"] == $danhsachkyhan[$j]["MaHTLanhLai"])		
								echo "<option selected value='{$danhsachhinhthuclanhlai[$j]["MaHTLanhLai"]}'>{$danhsachhinhthuclanhlai[$j]["TenHTLanhLai"]}</option>";
							else 
								echo "<option value='{$danhsachhinhthuclanhlai[$j]["MaHTLanhLai"]}'>{$danhsachhinhthuclanhlai[$j]["TenHTLanhLai"]}</option>";
						}
					?>
				</select>
	        </td>  
	        <td style="padding-top:10px;">
	        	<label for="ngaymoso">Ngày lãnh lãi:</label>
	        </td>
			<td style="padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="NgayLanhLai" name="NgayLanhLai" class="txt" size="12" value="0"  />
			</td>  
		</tr>
		<tr>
			<td style="padding-top:10px;">
	        	<label for="ngaymoso">Ngày mở sổ:</label>
	        </td>
			<td style="padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="NgayMoSo" name="NgayMoSo" class="txt" size="12" value="0" />
			</td>
			<td style="padding-top:10px;">
				<label for="ngaydenhan">Ngày đến hạn:</label>
	        </td>
			<td style="padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="NgayDenHan" name="NgayDenHan" class="txt" size="12" value="0" />
	        </td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="sotiengui">Số tiền gửi:</label>&nbsp<span class="requirefield">*</span> 
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <input placeholder ="Nhập số tiền gửi" tabindex="1" type="text" name="SoTienGui" class="txt" size="54" required oninvalid="this.setCustomValidity('Bạn chưa số tiền gửi')" onchange="this.setCustomValidity('')"/>   
	        </td>
	        <td style="width:15px;padding-top:10px;">
				<label for="loaitiengui">Loại tiền gửi:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
				<input readonly="readonly" tabindex="1" type="text" id="LoaiTienGui" name="LoaiTienGui" class="txt" size="12" value="VND" />
	        </td>       
		</tr>
		<tr>
			<td colspan="0">
			<hr>
			</td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="sobuttoan">Số bút toán:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	            <input readonly="readonly" tabindex="1" type="text" name="SoButToan" class="txt" size="54" value="<?php include 'getsobuttoan.php';?>"/>   
	        </td>
	        <td style="width:20px;padding-top:10px;">

	        </td>
	        <td style="width:60px;padding-top:10px;">

	        </td>
		</tr>
		<tr>
			<td style="width:15px;padding-top:10px;">
	        	<label for="nhanvienlapso">Nhân viên lập sổ:</label>
	        </td>
	        <td style="width:60px;padding-top:10px;">
	        	<?php 
	        		$nguoidung = new nguoidungbs();
	        		$nguoidung->MaNguoiDung = $_SESSION["MaND"];
	        		$danhsach = $nguoidung->LayDanhSach();
	        		$HoTen = $danhsach[0]["Ho"]." ".$danhsach[0]["Ten"];
	        	?>
	        	<input type="hidden" name="MaNhanVien" value="<?php echo $danhsach[0]['MaNguoiDung'] ?>"/>
	            <input readonly="readonly" tabindex="1" type="text" name="TenNhanVien" class="txt" size="54" value="<?php echo $HoTen; ?>"/>   
	        </td>
	        <td style="width:15px;padding-top:10px;">
	        	<label for="loaigiaodich">Loại giao dịch:</label>&nbsp<span class="requirefield">*</span>   
	        </td>
	        <td style="width:60px;padding-top:10px;">
	        	<select name="MaLoaiGiaoDich">		
            		<option value="0">-- Chọn loại giao dịch --</option>	                      	
					<?php
						$loaigiaodich = new loaigiaodichbs();
						$dsloaigiaodich= $loaigiaodich->LayDanhSach();
						for($j = 0; $j < ($dsloaigiaodich != false ? count($dsloaigiaodich) : 0); $j++)
						{		
							echo "<option value='{$dsloaigiaodich[$j]["MaLoaiGiaoDich"]}'>{$dsloaigiaodich[$j]["TenLoaiGiaoDich"]}</option>";
						}
					?>
				</select>
	        </td>
		</tr>
		<p class="requirefields">(*): Các trường bắt buộc phải có.</p>
</table>
<br/>
<br/>
<p align="center">
	<input type="submit" class="button small blue" value="In phiếu gửi tiền"/>
	&nbsp &nbsp &nbsp
	<a href="javascript:document.forms['formgui'].reset();" class="button small blue">Nhập lại</a>	
</p>
</form>
<br/>
<br/>
<a href="index.php?do=khachhang">Quay về danh sách khách hàng</a>
<?php
	if(isset($_SESSION["LoiMoSoTietKiem"]))
	{
		echo $_SESSION["LoiMoSoTietKiem"];
		unset($_SESSION["LoiMoSoTietKiem"]);
	}
?>

<script type="text/javascript">
function checkPostArtForm(frm)
{
	if(frm.TenKhachHang.value==0)
	{
		alert('Bạn chưa chọn khách hàng!');
		frm.TenKhachHang.focus();
		return false;
	}
	if(frm.MaKyHan.value==0)
	{
		alert('Bạn chưa chọn kỳ hạn!');
		frm.MaKyHan.focus();
		return false;
	}
	if(frm.MaHTLanhLai.value==0 && frm.MaHTLanhLai.disabled ==false)
	{
		alert('Bạn chưa chọn hình thức lãnh lãi!');
		frm.MaHTLanhLai.focus();
		return false;
	}
	if(frm.SoTienGui.value=="")
	{
		alert('Bạn chưa nhập số tiền gửi!');
		frm.SoTienGui.focus();
		return false;
	}
	var numberRe = /^[0-9]+$/;
	if(!frm.SoTienGui.value.match(numberRe))
	{	
		alert('Số tiền gửi không hợp lệ.');
		frm.SoTienGui.focus();
		return false;
	}

	if(frm.SoTienGui.value < 500000|| frm.SoTienGui.value > 1000000000)
	{
		alert('Số tiền gửi phải >= 500 ngàn và <= 1 tỷ đồng!');
		frm.SoTienGui.focus();
		return false;
	}
	
	if(frm.MaLoaiGiaoDich.value==0)
	{
		alert('Bạn chưa chọn loại giao dịch');
		frm.MaLoaiGiaoDich.focus();
		return false;
	}

	var returnValue = confirm("Bạn chắc là thông tin mở sổ tiết kiệm nhập đúng?");
    
    if(returnValue == true)
    {
        alert("Tiến hành in phiếu gửi tiền!");
    }
    else
    {
        return false;
    }
    
    return true;
}
</script>
